﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnitShared;

using TomShane.Neoforce.Controls;

using tactics.Interfaces;

namespace tactics.GUI
{
    using AbilityList       = List<Abilities.Ability>;
    using AbilityCardList   = List<AbilityCard>;
    using Coords            = Utility.Pair<int, int>;

    public class AbilityCardManager
    {
        Manager guiManager;
        string  xmlFile;

        AbilityList     abilities;
        AbilityCardList abilityCards;

        Coords coords;

        public AbilityCardManager(Manager guiManagerIn, string xmlFileIn, AbilityList abilitiesIn, int x, int y)
        {
            guiManager = guiManagerIn;
            xmlFile = xmlFileIn;

            abilities = abilitiesIn;
            coords = new Coords(x, y);

            InitCards();
        }

        public void Remove()
        {
            foreach (var card in abilityCards)
                card.Remove();
        }

        private void InitCards()
        {
            abilityCards = new AbilityCardList();

            for (int i = 0; i < abilities.Count; ++i)
            {
                Abilities.Ability current = abilities[i];
                abilityCards.Add(new AbilityCard(guiManager, xmlFile, current));
            }
        }

        public void UpdatePosition(int x, int y)
        {
            coords.First = x;
            coords.Second = y;
        }

        public void ShowCardsForUnit(Unit unit)
        {
            // turning on cards for a unit
            if (unit != null && unit.Abilities != null)
            {
                int y = 0;
                // foreach ability in unit
                foreach (UnitTemplate.AbilityUses abilityId in unit.Abilities)
                {
                    AbilityCard card = GetCard(abilityId.Id);
                    if (card != null)
                    {
                        card.Visible = true;
                        card.UpdatePosition(coords.First, coords.Second + (y * (card.Height + 2)));
                    }
                    ++y;
                }

            }
            // turning off all cards
            else
                HideCards();
        }

        public void HideCards()
        {
            foreach (AbilityCard card in abilityCards)
                card.Visible = false;
        }

        public AbilityCard GetCard(int id)
        {
            AbilityCard ret = null;

            if (id >= 0 && id <= abilityCards.Count)
            {
                for (int i = 0; i < abilityCards.Count; ++i)
                    if (abilityCards[i].Ability.Id == id)
                        ret = abilityCards[i];
            }

            return ret;
        }
    }
}